package org.nzj.useradmin.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.nzj.useradmin.vo.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class RegisterAction extends ActionSupport implements ModelDriven<User> {
	private List<File> file;// 封装上传文件
	private List<String> fileContentType;// 上传文件的类型
	private List<String> fileFileName;// 设置上传文件的文件名
//-------------------表单内容
	private User user;
	public String register() throws Exception {
		//表单域字段值
		System.out.println(user.getName());
		
		//设置路径
		String path = ServletActionContext.getServletContext().getRealPath("upload");
		//循环读取上传文件 
		if (null!=this.getFile()) {
		for (int i = 0; i < this.getFile().size(); i++) {
			//得到文件对象
			File f = new File(path,this.getFileFileName().get(i));
			//文件输出流
			FileOutputStream os = new FileOutputStream(f);
			//文件输入流
			FileInputStream is = new FileInputStream(this.getFile().get(i));
			byte[] buffer = new byte[(int)this.getFile().get(i).length()];
			int len=0;
			while ((len=is.read(buffer))>0){
				os.write(buffer,0,len);
			}
			os.close();
			is.close();
		}
		}
//		System.out.println("success");
		return SUCCESS;
	}
	public List<File> getFile() {
		return file;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public User getModel() {
          if (null == user) {
                    return user = new User();
          }
          return user;
}



	public void setFile(List<File> file) {
		this.file = file;
	}



	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}


	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}




}
